﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccessLayer
{
    public class clsSQLTranslation
    {
        #region Properties
        public clsDatabase.enumDatabase eDatabase { get; set; }
        public String IDField { get; set; }
        public String ActualDate { get; set; }
        public String MoneyField { get; set; }
        private String DateTimeField { get; set; }
        #endregion

        #region Constructors
        /// <summary>
        /// Translate SQL into database specific SQL.
        /// </summary>
        /// <param name="pDatabase"></param>
        public clsSQLTranslation(clsDatabase.enumDatabase pDatabase)
        {
            this.eDatabase = pDatabase;

            switch (this.eDatabase)
            {
                case clsDatabase.enumDatabase.eSQLServer2000:
                    this.IDField = "ID INT PRIMARY KEY IDENTITY, ";
                    this.ActualDate = "GETDATE()";
                    this.MoneyField = "MONEY";
                    break;
                case clsDatabase.enumDatabase.eMySQL:
                    this.IDField = "ID INT KEY NOT NULL AUTO_INCREMENT, ";
                    this.ActualDate = "CURRENT_DATE()";
                    this.MoneyField = "DECIMAL";
                    break;
                default:
                    throw new ArgumentException("Error 6: Database not defined.");
            }
        }
        #endregion

        #region Events
        #endregion

        #region Public methods
        /// <summary>
        /// Converts a DateTime object to a database date field.
        /// 
        /// </summary>
        /// <param name="pDate"></param>
        /// <returns></returns>
        public String GetDateString(DateTime pDate)
        {
            String DateString = "";
            switch (this.eDatabase)
            {
                case clsDatabase.enumDatabase.eSQLServer2000:
                    DateString = "'" + pDate.ToString("yyyyMMdd HH:mm:ss") + "'";
                    break;
                case clsDatabase.enumDatabase.eMySQL:
                    DateString = "'" + pDate.ToString("yyyy/MM/dd HH:mm:ss") + "'";
                    break;
                default:
                    break;
            }
            return DateString;
        }

        /// <summary>
        /// Get the right datefield for a SELECT statement.
        /// </summary>
        /// <param name="pFieldName">Name of the field.</param>
        /// <returns></returns>
        public String GetDateField(String pFieldName)
        {
            switch (this.eDatabase)
            {
                case clsDatabase.enumDatabase.eSQLServer2000:
                    this.DateTimeField = pFieldName;
                    break;
                case clsDatabase.enumDatabase.eMySQL:
                    this.DateTimeField = "DATE_FORMAT(" + pFieldName + ", '%Y/%m/%d %H:%i') AS " + pFieldName;
                    break;
                default:
                    throw new ArgumentException("Error 6: Database not defined.");
            }
            return this.DateTimeField;
        }
        #endregion

        #region Private methods
        #endregion
    }
}
